package show.marvelous.domain.chessgame.repository;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import show.marvelous.domain.chessgame.aggregate.entity.DiceRecord;
import show.marvelous.domain.chessgame.aggregate.entity.DiceRecordBuilder;
import show.marvelous.domain.chessgame.constans.PieceCodeEnum;
import show.marvelous.infrastructure.dao.chessgame.DiceRecordDO;
import show.marvelous.infrastructure.dao.chessgame.DiceRecordDOBuilder;
import show.marvelous.infrastructure.dao.chessgame.IDiceRecordDao;
import show.marvelous.infrastructure.dao.chessgame.IPlayRecordDao;
import show.marvelous.infrastructure.dao.chessgame.PlayRecordDO;

/**
 * @author Kris
 * 2021/8/27
 */
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DiceRecordRepository{

    private final IDiceRecordDao diceRecordDao;
    private final IPlayRecordDao playRecordDao;

    public void save(long gameId, DiceRecord diceRecord){
        DiceRecordDO record = DiceRecordDOBuilder.diceRecordDO().id(diceRecord.getId()).gameId(gameId)
                .color(diceRecord.getColor()).step(diceRecord.getStep()).build();
        diceRecordDao.save(record);
    }

    public Collection<DiceRecord> listDiceRecord(long gameId){
        List<PlayRecordDO> playRecordDOList = playRecordDao.listByGameId(gameId);
        Map<Long,PieceCodeEnum> diceRecordPieceCodeMap = playRecordDOList.stream()
                .collect(Collectors.toMap(PlayRecordDO::getDiceRecordId, PlayRecordDO::getCode));
        return diceRecordDao.listByGameId(gameId).stream()
                .map(r -> DiceRecordBuilder.diceRecord().id(r.getId()).color(r.getColor()).step(r.getStep())
                        .code(diceRecordPieceCodeMap.get(r.getId())).build()).collect(Collectors.toList());
    }
}
